//
//  TimePickerViewController.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-20.
//  Copyright 2009 Think Small Studio. All rights reserved.
//

#import "TimePickerViewController.h"


@implementation TimePickerViewController
@synthesize timePicker;
@synthesize editingItem;
@synthesize row;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Set the view background to match the grouped tables in the other views.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	// Set the right button Save
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
																							target:self action:@selector(save:)] autorelease];
}

// Call this function whenever the view is going to appear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	// Set the title and initial begin/end time
	if (self.row == 1) {
		self.title = @"Begin Time";
		timePicker.date = [editingItem valueForKey:@"begintime"];
	}
	else if (self.row == 2) {
		self.title = @"End Time";
		timePicker.date = [editingItem valueForKey:@"endtime"];
	}
	else {
		// never happen
	}
	
	self.navigationItem.rightBarButtonItem.enabled = NO;
}

// Call this when you hit the Save button
- (IBAction)save:(id)sender {
	
    // Save the title and begin/end time
	if (self.row == 1) {
		self.title = @"Begin Time";
		[editingItem setValue:timePicker.date forKey:@"begintime"];
	}
	else if (self.row == 2) {
		self.title = @"End Time";
		[editingItem setValue:timePicker.date forKey:@"endtime"];
	}
	else {
		// never happen
	}
	
    // Pop the view to return to the main list
    [self.navigationController popViewControllerAnimated:YES];
}

// Call this when you manipulate the date picker
- (IBAction)timeChanged:(id)sender {
	self.navigationItem.rightBarButtonItem.enabled = YES;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[timePicker release];
	[editingItem release];
    [super dealloc];
}


@end
